package lxt.action;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import lxt.action.core.ActionSupport;
import lxt.action.core.DateUtil;
import lxt.entity.CostFine;
import lxt.entity.DriverInfo;
import lxt.entity.InvoiceOrder;
import lxt.entity.TruckInfo;
import lxt.entity.UserInfo;
import lxt.service.CostFineService;
import lxt.service.DriverService;
import lxt.service.InvoiceOrderService;
import lxt.service.TruckService;

/**
 * 违章罚款管理action
 * @author Lit
 *
 */
public class CostFineAction extends ActionSupport{
	
	private static final long serialVersionUID = 1L;
	 private String id;
     private String truckId;
     private String orderId;
     private String driverId;
     private String illegalType;
     private String location;
     private String fineCharge;
     private String illegalDate;
     private String note;
     private Date entryTime;
     private String entryUser;
	//定义service
    private CostFineService costFineService;
    private DriverService driverService ; //用户获取驾驶员
    private TruckService truckService ; //用于获取车辆
    private InvoiceOrderService invoiceOrderService ;//用于获取订单号
    //定义对象
	private List<CostFine> costFines;
	private List<TruckInfo> trucks; //车牌号list
	private List<InvoiceOrder> invoiceOrders ; //订单号list
	private List<DriverInfo> drivers ; //驾驶员list
	
	private String deleteIds;
	//定义分页变量
	private Integer pageCounts = 10; //每页显示数量
	private String currPageStr = "1"; //分页
	private Integer totalCounts ;//总数据量
    
	/**
	 * 加载
	 * @return
	 */
	public String loadAll(){
		try{
			//定义从第几条显示
			int offset =( Integer.parseInt(currPageStr)-1)*pageCounts;
			//获得交通事故列表
			costFines = costFineService.loadCostFine(truckId, driverId, offset, pageCounts);
			//获取总的记录数
			totalCounts = costFineService.loadAllCostFineCount(truckId, driverId);
		}catch(Exception ex){
			ex.printStackTrace();
			this.addActionMessage("操作失败，请检查重新操作");
		}
		return SUCCESS;
	}
	/**
	 * 添加预处理
	 * @return
	 */
	public String AddPre(){
		try{
			trucks = truckService.loadAll();
			drivers = driverService.loadAll();
			invoiceOrders = invoiceOrderService.loadAll();
		}catch(Exception ex){
			ex.printStackTrace();
			this.addActionMessage("操作失败，请检查重新操作");
		}
		return SUCCESS;
	}
	/**
	 * 添加
	 * @return
	 */
	public String add(){
		try{
			//创建新的对象
			CostFine costFine = new CostFine();
			//为对象赋值
			costFine.setDriverId(driverId);
			UserInfo user = (UserInfo)this.getSession().get("user");
			costFine.setEntryUser(user.getUsername());
			if(illegalDate!=null&&!illegalDate.equals("")){
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
				costFine.setIllegalDate(sdf.parse(illegalDate));
			}
			if(fineCharge!=null&&!fineCharge.equals("")){
				costFine.setFineCharge(Double.parseDouble(fineCharge));
			}
			costFine.setIllegalType(illegalType);
			costFine.setLocation(location);
			costFine.setNote(note);
			costFine.setOrderId(orderId);
			costFine.setTruckId(truckId);
			
			this.costFineService.addCostFine(costFine);
		}catch(Exception ex){
			ex.printStackTrace();
			this.addActionMessage("操作失败，请检查重新操作");
		}
		return SUCCESS;
	}
	/**
	 * 编辑预处理
	 * @return
	 */
	public String updatePre(){
		try{
			trucks = truckService.loadAll();
			drivers = driverService.loadAll();
			invoiceOrders = invoiceOrderService.loadAll();
			//获得参数
			CostFine costFine = this.costFineService.loadCostFineById(id);
			/**
			 * 查看用户操作时间 设置编辑权限
			 */
			UserInfo user = (UserInfo) this.getSession().get("user");
			if(user.getLoginType()!=0){//记录员
				Date preTime = costFine.getEntryTime();
				int falg = DateUtil.getDays(preTime);
				if(falg!=0){
					this.addActionMessage("无权修改数据,请联系管理员");
					this.loadAll();
					return INPUT;
				}
			}
			//为页面属性赋值
			this.driverId = costFine.getDriverId();
			this.fineCharge =String.valueOf(costFine.getFineCharge());
			this.id = costFine.getId();
			this.illegalType = costFine.getIllegalType();
			this.illegalDate = costFine.getIllegalDate().toString();
			this.location = costFine.getLocation();
			this.note = costFine.getNote();
			this.orderId = costFine.getOrderId();
			this.truckId = costFine.getTruckId();
		}catch(Exception ex){
			ex.printStackTrace();
			this.addActionMessage("操作失败，请检查重新操作");
		}
		return SUCCESS;
	}
	/**
	 * 修改
	 * @return
	 */
	public String update(){
		try{
			//重新获取修改目标
			CostFine costFine = this.costFineService.loadCostFineById(id);
			//更新目标对象的目标修改属性(部分属性)
			costFine.setDriverId(driverId);
			UserInfo user = (UserInfo)this.getSession().get("user");
			costFine.setEntryUser(user.getUsername());
			if(illegalDate!=null&&!illegalDate.equals("")){
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
				costFine.setIllegalDate(sdf.parse(illegalDate));
			}
			if(fineCharge!=null&&!fineCharge.equals("")){
				costFine.setFineCharge(Double.parseDouble(fineCharge));
			}
			costFine.setIllegalType(illegalType);
			costFine.setLocation(location);
			costFine.setNote(note);
			costFine.setOrderId(orderId);
			costFine.setTruckId(truckId);
			this.costFineService.updateCostFine(costFine);
		}catch(Exception ex){
			ex.printStackTrace();
			this.addActionMessage("操作失败，请检查重新操作");
		}
		return SUCCESS;
	}
	/**
	 * 删除
	 * @return
	 */
	public String delete(){
		try{
			/**
			 * 查看用户操作时间 设置编辑权限
			 */
			UserInfo user = (UserInfo) this.getSession().get("user");
			if(user.getLoginType()!=0){//记录员
				for(int i=0;i<deleteIds.length();i++){
					String[] delId = deleteIds.split(",");
					CostFine c = this.costFineService.loadCostFineById(delId[i]);
					int falg = DateUtil.getDays(c.getEntryTime());
					if(falg!=0){
						this.addActionMessage("无权修改数据,请联系管理员");
						this.loadAll();
						return INPUT;
					}
				}
			}
			this.costFineService.deleteCostFine(deleteIds);
		}catch(Exception ex){
			ex.printStackTrace();
			this.addActionMessage("操作失败，请检查重新操作");
		}
		return SUCCESS;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getTruckId() {
		return truckId;
	}
	public void setTruckId(String truckId) {
		this.truckId = truckId;
	}
	public String getOrderId() {
		return orderId;
	}
	public void setOrderId(String orderId) {
		this.orderId = orderId;
	}
	public String getDriverId() {
		return driverId;
	}
	public void setDriverId(String driverId) {
		this.driverId = driverId;
	}
	public String getIllegalType() {
		return illegalType;
	}
	public void setIllegalType(String illegalType) {
		this.illegalType = illegalType;
	}
	public String getLocation() {
		return location;
	}
	public void setLocation(String location) {
		this.location = location;
	}
	public String getFineCharge() {
		return fineCharge;
	}
	public void setFineCharge(String fineCharge) {
		this.fineCharge = fineCharge;
	}
	public String getIllegalDate() {
		return illegalDate;
	}
	public void setIllegalDate(String illegalDate) {
		this.illegalDate = illegalDate;
	}
	public String getNote() {
		return note;
	}
	public void setNote(String note) {
		this.note = note;
	}
	public Date getEntryTime() {
		return entryTime;
	}
	public void setEntryTime(Date entryTime) {
		this.entryTime = entryTime;
	}
	public String getEntryUser() {
		return entryUser;
	}
	public void setEntryUser(String entryUser) {
		this.entryUser = entryUser;
	}
	public CostFineService getCostFineService() {
		return costFineService;
	}
	public void setCostFineService(CostFineService costFineService) {
		this.costFineService = costFineService;
	}
	public DriverService getDriverService() {
		return driverService;
	}
	public void setDriverService(DriverService driverService) {
		this.driverService = driverService;
	}
	public TruckService getTruckService() {
		return truckService;
	}
	public void setTruckService(TruckService truckService) {
		this.truckService = truckService;
	}
	public InvoiceOrderService getInvoiceOrderService() {
		return invoiceOrderService;
	}
	public void setInvoiceOrderService(InvoiceOrderService invoiceOrderService) {
		this.invoiceOrderService = invoiceOrderService;
	}
	public List<CostFine> getCostFines() {
		return costFines;
	}
	public void setCostFines(List<CostFine> costFines) {
		this.costFines = costFines;
	}
	public List<TruckInfo> getTrucks() {
		return trucks;
	}
	public void setTrucks(List<TruckInfo> trucks) {
		this.trucks = trucks;
	}
	public List<InvoiceOrder> getInvoiceOrders() {
		return invoiceOrders;
	}
	public void setInvoiceOrders(List<InvoiceOrder> invoiceOrders) {
		this.invoiceOrders = invoiceOrders;
	}
	public List<DriverInfo> getDrivers() {
		return drivers;
	}
	public void setDrivers(List<DriverInfo> drivers) {
		this.drivers = drivers;
	}
	public String getDeleteIds() {
		return deleteIds;
	}
	public void setDeleteIds(String deleteIds) {
		this.deleteIds = deleteIds;
	}
	public Integer getPageCounts() {
		return pageCounts;
	}
	public void setPageCounts(Integer pageCounts) {
		this.pageCounts = pageCounts;
	}
	public String getCurrPageStr() {
		return currPageStr;
	}
	public void setCurrPageStr(String currPageStr) {
		this.currPageStr = currPageStr;
	}
	public Integer getTotalCounts() {
		return totalCounts;
	}
	public void setTotalCounts(Integer totalCounts) {
		this.totalCounts = totalCounts;
	}
	
}
